using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace Samples.Radio.Data
{
    /// <summary>
    /// Base class that implements INotifyPropertyChanged interface
    /// </summary>
    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raise the PropertyChanged event
        /// </summary>
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Raise the PropertyChanged event based on some expression (extract the 
        /// name of the property to ensure strongly typedness)
        /// </summary>
        public void RaisePropertyChanged<TValue>(Expression<Func<TValue>> propertySelector)
        {
            var memberExpression = propertySelector.Body as MemberExpression;
            if (memberExpression != null)
            {
                RaisePropertyChanged(memberExpression.Member.Name);
            }
        }
    }
}